package com.example.administrator.swredeem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.swredeem.R;
import com.example.administrator.swredeem.appBase.Config;
import com.example.administrator.swredeem.beanClass.MoneyBean;
import com.example.administrator.swredeem.net.HttpXutils;
import com.example.administrator.swredeem.net.MyCallback;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShowRechargeActivity extends AppCompatActivity {
    Intent intent = null;
    String title, userid, money, quantity, factory = "";
    @BindView(R.id.fwl_type)
    TextView fwlType;
    @BindView(R.id.fwf_money)
    TextView fwfMoney;
    @BindView(R.id.dj_o)
    TextView djO;
    @BindView(R.id.ql_o)
    TextView qlO;
    @BindView(R.id.je_o)
    TextView jeO;
    @BindView(R.id.dj_t)
    TextView djT;
    @BindView(R.id.ql_t)
    TextView qlT;
    @BindView(R.id.je_t)
    TextView jeT;
    @BindView(R.id.dj_three)
    TextView djThree;
    @BindView(R.id.ql_three)
    TextView qlThree;
    @BindView(R.id.je_three)
    TextView jeThree;
    @BindView(R.id.zql)
    TextView zql;
    @BindView(R.id.zje)
    TextView zje;


    private MoneyBean moneyBean;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_recharge);
        ButterKnife.bind(this);
        intent = getIntent();
        title = intent.getStringExtra("title");
        userid = intent.getStringExtra("userid");
        money = intent.getStringExtra("money");
        quantity = intent.getStringExtra("quantity");
        factory = intent.getStringExtra("factory");

        LinearLayout linearLayout = null;
        linearLayout = findViewById(R.id.buka);

        if (!TextUtils.isEmpty(title)) {
            if (title.equals("开卡")) {
                linearLayout.setVisibility(View.GONE);
            } else {
                linearLayout.setVisibility(View.VISIBLE);
            }
        } else {
            linearLayout.setVisibility(View.GONE);
        }

        init();
    }

    private void init() {
        moneyBean = new MoneyBean();
        gson = new Gson();
        net();
    }

    public void goQRcode(View v) {

        Intent intent1 = new Intent(ShowRechargeActivity.this, QRcodeActivity.class);
        intent1.putExtra("title", title);
        intent1.putExtra("userid", userid);
        intent1.putExtra("iccardno", "");
        intent1.putExtra("gascount", quantity);
        intent1.putExtra("gasfee", money);
        intent1.putExtra("factory", factory);
        startActivity(intent1);

    }


    public void net() {
        Map<String, String> map = new HashMap<>();
        map.put("spotno", "1");
        map.put("operno", "1");
        map.put("orgcode", "1");
        map.put("dataorgid", "1");
        map.put("userid", userid);
        map.put("cardid ", "");
        map.put("amount", quantity);
        map.put("gasfee", "");

//        Log.d("TAG", "init: =" + TextUtils.isEmpty(huhao) + TextUtils.isEmpty(idCrad));
        HttpXutils.getData(Config.Money, map, new MyCallback() {
            @Override
            public void onSuccess(String result) {
                Log.d("TAG", "onSuccess: " + result);
                moneyBean = gson.fromJson(result, MoneyBean.class);
                if (moneyBean.getInfo().size() > 0) {
                    assignmentData();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }


    public void assignmentData() {
        zql.setText("总气量:" + moneyBean.getAllamount());
        zje.setText("总金额:" + moneyBean.getAllpayfee());
        money = moneyBean.getAllpayfee();
        List<MoneyBean.InfoBean> elementsBean = moneyBean.getInfo();
        Log.d("TAG", "assignmentData: " + elementsBean.size());
        if (moneyBean.getInfo().size() >= 1) {
            djO.setText(Integer.parseInt(elementsBean.get(0).getPrice())/100);
            qlO.setText(elementsBean.get(0).getAmount());
            jeO.setText(Integer.parseInt(elementsBean.get(0).getPayfee())/100);
        }
        if (moneyBean.getInfo().size() >= 2) {
            djT.setText(Integer.parseInt(elementsBean.get(1).getPrice())/100);
            qlT.setText(elementsBean.get(1).getAmount());
            jeT.setText(Integer.parseInt(elementsBean.get(1).getPayfee())/100);
        }
        if (moneyBean.getInfo().size() >= 3) {
            djThree.setText(Integer.parseInt(elementsBean.get(2).getPrice())/100);
            qlThree.setText(elementsBean.get(2).getAmount());
            jeThree.setText(Integer.parseInt(elementsBean.get(2).getPayfee())/100);
        }


//        qlO.setText(elementsBean.get(0).get);

    }

}
